// JavaScript Document
$(function () {
	initAdd();
	//提交
	$(".submit").on("click",function(){
		checkAdd();
	});
	//图片文件内容改变
	$(".uploadFile").on("change",function(){
		$(".imgurl").val($(this).val());
        var reader = new FileReader();
        reader.readAsDataURL(this.files[0]);
        reader.onload = function(e){
            var strbase64 = e.target.result;
            var a = strbase64.split("base64,");
            $('.file_base64').val(a[1]);
        };
        preImg();//显示图片
	});
	//点击浏览图片
	$(".scan").on("click",function(){
		$(".uploadFile").click();	
	});
});
//初始化
function initAdd(){
	CKEDITOR.replace( 'crpopDesc' , { height: '240px', 
		width: '480px',
		toolbar: [['Source','-','Preview'],
		          ['Cut','Copy','Paste','PasteText','PasteFromWord'],
		          ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
		          ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
		          ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
		           ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
		           ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
		           ['Link','Unlink'],
		          ['Image','Table','HorizontalRule','Smiley'],
		           ['Styles','Format','Font','FontSize'],
		           ['TextColor','BGColor']],
		extraPlugins: 'codesnippet',
		uiColor: '#f3f3f3'
	} );
	$(".crpopTitle").val("");
	$(".crpopFee").val("");
	$(".crpopStock").val("");
	$(".crpopArea").val("");
	goodColumns();
}

//加载分类列表下拉框内容
function goodColumns(){
	var str = 'pageNum=1&pageSize=100';
	getOData(str,"coreArea/find/by/cnd",{fn:function(oData){
		var strhtml_goodColumns = '';
		var arrData = oData.data.result;
		var ln = arrData.length;
		for(var i=0;i<ln;i++){
			var craraTopCH="";
			var craraTop=arrData[i].craraTop;
			if(7==craraTop){
				craraTopCH="管家商城";
			} else if(8==craraTop){
				craraTopCH="鲜果到家";
			} else if(5==craraTop){
				craraTopCH="智能家居";
			}
			strhtml_goodColumns += '<option value ="'+arrData[i].craraUuid+'" >'+craraTopCH+'|'+arrData[i].craraName+'</option>';
		}
		$(".crpopArea").html(strhtml_goodColumns);
	}});
}
//检查提交
function checkAdd(){	
	if($.trim($(".crpopTitle").val()) == ""){
		alert("商品名称不能为空，请填写完再提交！");
		$(".crpopTitle").focus();
		return false;
	}
	if($.trim(CKEDITOR.instances.crpopDesc.getData()) == ""){
		alert("内容不能为空，请填写完再提交！");
		return false;
	}
	if($.trim($(".crpopFee").val()) == ""){
		alert("价格不能为空，请填写完再提交！");
		$(".crpopFee").focus();
		return false;
	}
	if($.trim($(".crpopStock").val()) == ""){
		alert("库存不能为空，请填写完再提交！");
		$(".crpopStock").focus();
		return false;
	}
	if($.trim($(".imgurl").val()) == ""){
		alert("图片附件不能为空，请填写完再提交！");
		$(".imgurl").focus();
		return false;
	}
	
	var r=confirm("是否确认增加？");
	if (r==true){
		var msgObject = parent.layer.msg('处理中，请等待……', {
			icon: 16,
			shade: 0.4,
			time: waitImgTime //（如果不配置，默认是3秒）
		}, function(index){
			//do something
			parent.layer.close(index); 
		});
		uploadPic(msgObject);
	}
}

//上传图片
function uploadPic(msgObject){
	$("#userForm").ajaxSubmit({
        //定义返回JSON数据，还包括xml和script格式
        type: "POST",
        dataType:'json',
        url: urlfile + "coreAttachment/upload",
        success: function(strdata) {
        	if(strdata.success==true){
        		Add(strdata.data,msgObject);
        	}else {
            	alert(strdata.errMsg);
            }
        }
    });
}
//提交
function Add(uuids,msgObject){
	var crpopTitle = $(".crpopTitle").val();
	var crpopFee = $(".crpopFee").val();
	var crpopStock = $(".crpopStock").val();
	var crpopArea = $(".crpopArea").val();
	var crpopDesc = CKEDITOR.instances.crpopDesc.getData();
	
	var str = 'crpopTitle='+encodeURIComponent(crpopTitle)+'&crpopFee='+encodeURIComponent(crpopFee)
	+'&crpopStock='+encodeURIComponent(crpopStock)+'&crpopArea='+encodeURIComponent(crpopArea)
	+'&crpopDesc='+encodeURIComponent(crpopDesc)+'&uuids='+encodeURIComponent(uuids);
	getOData(str,"corePointsProduct/add/pointsproduct",{
		fn:function(oData){
			window.parent.refreshList();
			alert("增加成功！");
		},
		fnerr:function(oData){
			parent.layer.close(msgObject);
		}
	});
}